package my2Cents;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Shape;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

import net.phys2d.math.Vector2f;
import net.phys2d.raw.shapes.PCircle;

public class ScrMessage {
	private String txt;
	private int alpha = 255;
	private int idealx = 0;
	private int idealy = 0;
	private double velx = 0;
	private double vely = 0;
	private int x = 0;
	private int y = 0;
	private double spd = .14;
	private int index = 0;
	private boolean zip = false;
	private int count = 0;
	private int tw;
	private int th;
	private BufferedImage icon;
	
	public void setTSize(int w, int h) {
		tw = w;
		th = h;
	}

	
	public ScrMessage(String txt, int index, int w, int h, String icon) {
		this.txt = txt;
		this.index = index;
		setTSize(w,h);
		applyIdealXY();
		y = idealy;
		x = tw;
		idealx = tw - 200;
		icon = "data/images/" + icon;
		loadIcon(new File(icon));
	}
	
	private void loadIcon(File file) {
		if(!file.exists()) return;
		try {
			icon = ImageIO.read(file);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public void think() {
		alpha = alpha - 1;
		alpha = Math.max(alpha,0);
		applyIdealXY();
	 	 
	 	y += (int) (vely * spd);
	 	x += (int) (velx * spd);

	 	int distx = idealx - x;
	 	velx += distx * spd;
//	 	if (Math.abs(distx) < 2 && Math.abs(velx) < 0.1) {
//	 		velx = 0; 
//	 	}
	 	
	 	int disty = idealy - y; 
	 	vely += disty * spd;
//	 	if (Math.abs(disty) < 2 && Math.abs(vely) < 0.1) {
//	 		vely = 0; 
//	 	}
	 	 
	 	velx = velx * 0.94;
	 	vely = vely * 0.94; 
		
	 	if(y > th) {
	 		this.alpha = 0;
	 	}
		if(alpha == 40) {
			zip = true;
			idealx = tw + 200;
		}
		if(alpha == 60) {
			idealx = tw - 400;
		}
		if(y < 0) {
			this.alpha = 0;
		}
	}
	
	public boolean zipped() {
		return zip;
	}
	
	private void applyIdealXY() {
		if(!zipped()) {
			idealy = (index*35)+80;
			if(count > 10 && index == 0) {
				idealy = -300;
			}
		}
	}

	public boolean shouldRemove() {
		return (this.alpha <= 0);
	}
	
	public int getAlpha() {
		return this.alpha;
	}
	
	public void setIndex(int i) {
		this.index = i;
	}
	
	public int getX() {
		return this.x;
	}
	
	public int getY() {
		return this.y;
	}
	
	public String getText() {
		return this.txt;
	}
	
	public void drawBG(Graphics g) {
		int size = 32;
		//g.setColor(Color.red);
		g.fillRect(x-(int)(size*1.3) - 20,(int)(y-size/1.5) - 20,250,size+40);
	}

	public void draw(Graphics g) {
		Graphics2D g2 = (Graphics2D) g;
		
		g.setColor(new Color(255,255,255,255));
		
		int size = 32;
		
		g.setColor(new Color(0,0,0));
		g.drawString(txt,x,y);
		
		
		
		g.drawRect(
				x - (int)(size*1.3), 
				y - (int)(size/1.5), 
				size, size);
	
		if(icon != null) {
			g.drawImage(icon, x - (int)(size*1.3), y - (int)(size/1.5), size, size, null);
		}
	}

	public void setCount(int count) {
		this.count  = count;
	}
}
